/**
 * GET请求；在header中添加参数
 * @param {String} url 接口地址
 * @param {JSON} header 请求头，包含参数，若无需参数则使用其他版本的get函数
 * @param {Function} funSuccess 请求成功的回调
 * @param {Function} funComplete 请求结束的回调，若无需处理则传空的方法体
 */
function getWithParams(url, header, funSuccess, funComplete = () => {}) {
	uni.showLoading({
		title: '加载中...',
		mask: false
	});
	uni.request({
		url: url,
		method: 'GET',
		header: header,
		sslVerify: false,
		success: res => {
			funSuccess(res)
		},
		fail: err => {
			console.log(err)
		},
		complete: () => {
			uni.hideLoading()
			funComplete()
		}
	});
}

/**
 * 无参的GET请求
 * @param {String} url 接口地址
 * @param {Function} funSuccess 请求成功的回调
 * @param {Function} funComplete 请求结束的回调，若无需处理则传空的方法体
 **/
function get(url, funSuccess, funComplete = () => {}) {
	uni.showLoading({
		title: '加载中...',
		mask: false
	});
	uni.request({
		url: url,
		method: 'GET',
		header: {
			"content-type": "application/json;charset=UTF-8"
		},
		sslVerify: false,
		success: res => {
			funSuccess(res)
		},
		fail: err => {
			console.log(err)
		},
		complete: () => {
			uni.hideLoading()
			funComplete()
		}
	});
}
/**
 * 无参的GET请求
 * @param {String} url 接口地址
 * @param {json} data要传递的参数  
 * @param {Function} funSuccess 请求成功的回调
 * @param {Function} funComplete 请求结束的回调，若无需处理则传空的方法体
 **/
function postParams(url, data, funSucess, funComplete = () => {}) {
	uni.showLoading({
		title: '提交....',
		mask: false
	});
	uni.request({
		url: url,
		header: {
			"accept": "application/json,*/*;q=0.1",
			"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
		},
		method: 'POST',
		data: data,
		success: res => {
			funSucess(res)
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading()
			funComplete()
		}
	});
}

function post(url, data, funSuccess, funComplete = () => {}) {
	uni.showLoading({
		title: '提交.....',
		mask: false
	});
	uni.request({
		url: url,
		header: {
			"content-type": "application/json;charset=UTF-8"
		},
		method: 'POST',
		data: data,
		sslVerify: false,
		success: res => {
			funSuccess(res.data)
		},
		fail: () => {


		},
		complete: () => {
			uni.hideLoading()
			funComplete()
		}
	});
}

/**
 *上传文件
 *@param {String} url 服务器地址 
 *@param {String} param 表单提交的name名称
 *@param {String} filePath 要上传的本地文件路径
 *@param {JSON} data 其他同时提交的表单参数，无表单参数则穿{}
 *@param {Function} funSuccess 成功的回调
 *@param {Function} funComplete 结束的回调，带默认参数不需则可不传 
 */
function postFile(url, param, filePath, data, funSuccess, funComplete = () => {}) {
	uni.showLoading({
		title: '提交中...',
		mask: false
	});
	uni.uploadFile({
		url: url,
		sslverify: false,
		filePath: filePath,
		name: param,
		formData: data,
		success: (res) => {
			funSuccess(res)

		},
		fail(err) {
			console.log(err);

		},
		complete() {
			uni.hideLoading()
			funComplete()
		}
	})
}

/**
 *以同步写法实现异步请求，在嵌套请求时，async块内部代码是同步的，会等待await代码行的执行结果
 *注意lambada表达式内使用时，async应在匿名函数参数前（需要加括号）
 * @param {String} url 请求地址
 * @param {JSON} data 请求参数,
 * @param {String}  
 *    */
function request(url, data = {}, method = 'GET') {
	return new Promise((resovle, reject) => {
		uni.request({
			url,
			method,
			data,
			success: res => {
				resovle(res.data)
			},
			fail: err => {
				reject(err)
			}
		});
	})
}

/**
 * 带参数的删除
 * @param {String} url 接口地址
 * @param {JSON} data 请求参数
 * @param {Function} funSuccess  FunSuccess 成功的回调
 */
function deleteParams(url, data, funSuccess) {
	uni.request({
		url,
		method: 'DELETE',
		data,
		header: {
			"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
		},
		success: res => {
			funSuccess(res.data)
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {}
	});
}
module.exports = {
	getWithParams,
	get,
	postParams,
	post,
	postFile,
	request,
	deleteParams
}
